﻿//
// Copyright (c) 2010
// by Ashish Agrawal and Chris Paterra
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using SimpleBlog.Components.Models;
using SimpleBlog.Components.Views;
using DotNetNuke.Web.Mvp;
using SimpleBlog.Components.Data;
using SimpleBlog.Components.Controllers;
using DotNetNuke.Common;
using System;
using SimpleBlog.Components.Common;
using SimpleBlog.Components.Entities;
using DotNetNuke.Common.Utilities;

namespace SimpleBlog.Components.Presenters
{

	/// <summary>
	/// 
	/// </summary>
	public class EditEntryPresenter : ModulePresenter<IEditEntryView, EditEntryModel> 
	{

		#region Private Members

		protected ISimpleBlogController Controller { get; private set; }

		/// <summary>
		/// 
		/// </summary>
		public int EntryID {
			get {
				var entryId = Null.NullInteger;
				if (!String.IsNullOrEmpty(Request.Params["EntryID"])) entryId = Int32.Parse(Request.Params["EntryID"]);
				return entryId;
			}
		}

		#endregion

		#region Constructor

		/// <summary>
		/// 
		/// </summary>
		/// <param name="view"></param>
		public EditEntryPresenter(IEditEntryView view) : this(view, new SimpleBlogController(new SqlDataProvider()))
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="view"></param>
		/// <param name="controller"></param>
		public EditEntryPresenter(IEditEntryView view, ISimpleBlogController controller) : base(view)
		{
			if (view == null) {
				throw new ArgumentException(@"View is nothing.", "view");
			}

			if (controller == null) {
				throw new ArgumentException(@"Controller is nothing.", "controller");
			}

			Controller = controller;
		}

		#endregion

		#region Events

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void Cancel(object sender, EventArgs e) {
			Response.Redirect(Globals.NavigateURL());
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void Delete(object sender, EventArgs e) {
			try {
				var objEntry = Controller.GetEntry(EntryID, ModuleId);

				if (objEntry != null)
				{
					var cntTaxonomy = new Taxonomy.Content();
					cntTaxonomy.DeleteContentItem(objEntry);

					Controller.DeleteEntry(EntryID, ModuleId);

					 Response.Redirect(Globals.NavigateURL());
				}
			} catch (Exception exception) {
				ProcessModuleLoadException(exception);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void Save(object sender, UIEventArgs<EntryInfo> e) {
			try {
				e.Entry.EntryId = EntryID;
				e.Entry.ModuleId = ModuleId;

				if (e.Entry.Publish) 
				{
					if (e.Entry.PublishOnDate == Null.NullDate) 
					{
						e.Entry.PublishOnDate = DateTime.Now;
					}
				}

				if (EntryID != Null.NullInteger)
				{
					e.Entry.LastModifiedByUserId = UserId;
                    //e.Entry.LastModifiedOnDate = DateTime.Now;
					Controller.UpdateEntry(e.Entry, TabId);
				}
				else
				{
					e.Entry.CreatedByUserId = UserId;
                    //e.Entry.CreatedOnDate = DateTime.Now;
					e.Entry.LastModifiedByUserId = UserId;
                    //e.Entry.LastModifiedOnDate = DateTime.Now;

					var objEntry = Controller.AddEntry(e.Entry, TabId);
					Controller.UpdateEntry(objEntry, TabId);
				}
				 Response.Redirect(Globals.NavigateURL());
			} catch (Exception exception) {
				ProcessModuleLoadException(exception);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		protected override void OnInit() {
			base.OnInit();

			try {
				if (EntryID != Null.NullInteger) 
				{
					var objBlog = Controller.GetEntry(EntryID, ModuleId);

					if (objBlog != null) 
					{
						View.Model.BlogEntry = objBlog;
						View.ShowAuditControl(true);
						View.ShowDeleteButton(true);
					} 
					else
						Response.Redirect(Globals.NavigateURL(), true);
				} 
				else 
				{
					View.Model.BlogEntry = new EntryInfo();
					View.ShowAuditControl(false);
					View.ShowDeleteButton(false);
				}

				View.Cancel += Cancel;
				View.Delete += Delete;
				View.Save += Save;
				View.Refresh();
			} catch (Exception exc) {
				ProcessModuleLoadException(exc);
			}
		}

		#endregion

	}
}